public class HashBucket2<K,V> {

    private static class Node<K,V> {
        private K key;
        private V value;
        Node<K,V> next;


        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }

    public Node<K,V>[] array;
    public int usedSize;
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public HashBucket2() {
        array = (Node<K, V>[]) new Node[DEFAULT_SIZE];
    }

    public void put(K key, V value) {
        int hashcode = key.hashCode();
        int index = Math.abs(hashcode % array.length);

        Node<K,V> cur = array[index];
        while (cur != null) {
            if(cur.key .equals(key)) {
                cur.value = value;
            }
            cur = cur.next;
        }

        //头插
        Node<K,V> node = new Node(key,value);
        node.next = array[index];
        array[index] = node;
        usedSize++;
    }

    public double loadFactor() {
        return usedSize * 1.0 / array.length;
    }

    public V get(K key) {
        int hashcode = key.hashCode();
        int index  = Math.abs(hashcode % array.length);
        Node<K,V> cur = array[index];

        while (cur != null) {
            if(cur.key .equals(key)) {
                return (V) cur.value;
            }
            cur = cur.next;
        }
        return null;
    }

}
